package com.funo.business.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.funo.business.dao.ProductDao;
import com.funo.business.dao.ProductItemDao;
import com.funo.business.entities.Product;
import com.funo.business.entities.ProductItem;
import com.funo.business.service.ProductService;
import com.funo.common.pojo.PageResult;
import com.funo.common.pojo.QueryCondition;
import com.funo.common.utils.Constant;
import com.funo.core.base.BaseService;

@Service("productService")
public class ProductServiceImpl extends BaseService implements ProductService{
	
	@Autowired
	@Qualifier("productDao")
	private ProductDao productDao;
	
	@Autowired
	@Qualifier("productItemDao")
	private ProductItemDao productItemDao;

	@Override
	public void delProduct(String productId) {
		// TODO Auto-generated method stub
		Product product = productDao.get(productId);
		product.setIsValid(0);
		productDao.update(product);
	}

	@Override
	public void delProductItem(String itemId) {
		// TODO Auto-generated method stub
		ProductItem item = productItemDao.get(itemId);
		item.setIsValid(0);
		productItemDao.update(item);
	}

	@Override
	public List<ProductItem> getItemsByUser(String userId) {
		// TODO Auto-generated method stub
		return productItemDao.getItems(userId);
	}

	@Override
	public Product getProduct(String productId) {
		// TODO Auto-generated method stub
		return productDao.get(productId);
	}

	@Override
	public ProductItem getProductItem(String itemId) {
		// TODO Auto-generated method stub
		return productItemDao.get(itemId);
	}

	@Override
	public PageResult<Product> listPage(QueryCondition condition,Product product) {
		// TODO Auto-generated method stub
		PageResult<Product> page = new PageResult<Product>();
		page.setList(productDao.pageList(condition,product));
		page.setTotal(productDao.count(condition,product));
		return page;
	}

	@Override
	public Integer saveProduct(Product product) {
		// TODO Auto-generated method stub
		product.setProductId(UUID.randomUUID().toString());
		productDao.save(product);
		return Constant.STATUS_SUSSESS;
	}

	@Override
	public Integer saveProductItem(ProductItem item) {
		// TODO Auto-generated method stub
		item.setProductItemId(UUID.randomUUID().toString());
		productItemDao.save(item);
		return Constant.STATUS_SUSSESS;
	}

	@Override
	public Integer updateProduct(Product product) {
		// TODO Auto-generated method stub
		Product pro = productDao.get(product.getProductId());
		BeanUtils.copyProperties(product, pro, new String[]{"productId"});
		productDao.update(pro);
		return Constant.STATUS_SUSSESS;
	}

	@Override
	public Integer updateProductItem(ProductItem item) {
		// TODO Auto-generated method stub
		ProductItem item2 = productItemDao.get(item.getProductItemId());
		BeanUtils.copyProperties(item, item2, new String[]{"productItemId"});
		productItemDao.update(item2);
		return Constant.STATUS_SUSSESS;
	}

}
